***Note: replicators should set directory to open data 
*cd ""


********** FIGURE 1: ETHNORACIAL PARADOX, panel a **********

clear all
use ESS.dta

* D and PD are discriminatory attitudes and perceptions of discrimination, here we focus on ethnoracial cleavage

gen D=D_race
gen PD=PD_race

collapse(mean) total_eduyrs PD D , by(country essround)
 
label var D "Discriminatory attitudes"
label var PD "Perceptions of discrimination"
 
*generate sub-labels for european areas
 
gen Continental=0
replace  Continent=1 if country=="Austria" |  country=="Belgium"  |  country=="Germany"   |   country=="France"   |  country=="Switzerland"  |  country=="Luxembourg"   |  country=="Netherlands"  
	
gen Scandinavia=0
replace  Scandinavia=1 if country=="Norway" 	|   country=="Denmark"   | country=="Sweden" |  country=="Finland" |   country=="Estonia"  |   country=="Iceland"
	
gen Mediterranean=0
replace  Mediterranean=1 if country=="Spain" |   country=="Greece" |   country=="Italy"    |  country=="Portugal"   

gen Anglo_Saxon=0
replace  Anglo_Saxon=1 if country=="Ireland" |   country=="United Kingdom"

gen Eastern=0
replace  Eastern=1 if   country=="Croatia" | country=="Czech Republic"  | country=="Lithuania" | country=="Poland" | country=="Russia" | country=="Slovak Republic"   | country=="Ukraine" | country=="Slovak Republic" | country=="Slovenia"


gen Area=.
replace Area=1 if Anglo_Saxon==1
replace Area=2 if Continental==1
replace Area=3 if Eastern==1
replace Area=4 if Mediterranean==1
replace Area=5 if Scandinavia==1

drop if Area==.

label variable Area "Subcontinent"
la def aria  1 "Anglo-Saxon" 2 "Continental" 3 "Eastern" 4 "Mediterranean" 5 "Nordic", modify
la val Area aria


 grstyle init
 grstyle set legend 2,  nobox
 grstyle set size 8pt: tick_label key_label
 grstyle set size 12pt: heading
 grstyle set size 10pt: subheading axis_title 
 grstyle set graphsize 13cm 11.5cm
 
 
*here we run OLS in order to retrieve the data to be printed in the graph note of Figure 1
reg  PD D
ereturn list
loc N6= e(N)
local b= round(_b[D],.001)
local t = _b[D]/_se[D]
local p =round(2*ttail(e(df_r),abs(`t')),.001)
 

   graph twoway    (lfitci PD  D,   lcol(black)  lwidth(medthick) ciplot(rline) blpattern(dash) acolor(black%65) )     (scatter PD  D if Area==1,       mcolor(black)   msymbol(d))   (scatter PD  D if Area==2,   mcolor(black)    msymbol(S)) (scatter PD  D if Area==3,   mcolor(black)      msymbol(o)) (scatter PD  D if Area==4,   mcolor(black)    msymbol(T)) (scatter PD  D if Area==5,   mcolor(black)    msymbol(D)), name(a, replace)    legend(nobox position(12) rows(1)  order(3 "Anglo-Saxon" 4 "Continental" 5 "Eastern" 6 "Mediterranean" 7 "Nordic") span)   yscale(titlegap(*10)) ytitle("Perceptions of discrimination", size(medlarge)) xtitle("Discriminatory attitudes") note("Plot country-round pairwise correlation. {&rho} = `b' | {it:p-value} = `p' | {it:N} = `N6'.", size(vsmall)) title("{bf:Ethnoracial attitudes}")
  graph save "Graph/Figure1a", replace 
  
 
********** FIGURE 1: GENDER PARADOX, panel b **********

*Same as 1a, we just adapt the code for gender

clear all
use "ESS.dta"

* D and PD are discriminatory attitudes and perceptions of discrimination, here we focus on gender cleavage

gen D=D_gender
gen PD=PD_gender
  
 collapse(mean) total_eduyrs PD D , by(country essround)
 
 label var D "Discrimination"
 label var PD "Perceived discrimination"

 
gen Continental=0
replace  Continent=1 if country=="Austria" |  country=="Belgium"  |  country=="Germany"   |   country=="France"   |  country=="Switzerland"  |  country=="Luxembourg"   |  country=="Netherlands"  
	
gen Scandinavia=0
replace  Scandinavia=1 if country=="Norway" 	|   country=="Denmark"   | country=="Sweden" |  country=="Finland" |   country=="Estonia"  |   country=="Iceland"
	
gen Mediterranean=0
replace  Mediterranean=1 if country=="Spain" |   country=="Greece" |   country=="Italy"    |  country=="Portugal"   

gen Anglo_Saxon=0
replace  Anglo_Saxon=1 if country=="Ireland" |   country=="United Kingdom"

gen Eastern=0
replace  Eastern=1 if   country=="Croatia" | country=="Czech Republic"  | country=="Lithuania" | country=="Poland" | country=="Russia" | country=="Slovak Republic"   | country=="Ukraine" | country=="Slovak Republic" | country=="Slovenia"

gen Area=.
replace Area=1 if Anglo_Saxon==1
replace Area=2 if Continental==1
replace Area=3 if Mediterranean==1
replace Area=4 if Scandinavia==1

label variable Area "Subcontinent"
la def aria  1 "Anglo-Saxon" 2 "Continental" 3 "Mediterranean" 4 "Scandinavia", modify
la val Area aria

   grstyle init
 grstyle set legend 2,  nobox
 grstyle set size 8pt: tick_label key_label
 grstyle set size 12pt: heading
 grstyle set size 10pt: subheading axis_title 
 grstyle set graphsize 13cm 11.5cm

 
reg  PD D
ereturn list
loc N6= e(N)
local b= round(_b[D],.001)
local t = _b[D]/_se[D]
local p =round(2*ttail(e(df_r),abs(`t')),.001)

 
  graph twoway    (lfitci PD  D,   lcol(black)  lwidth(medthick) ciplot(rline) blpattern(dash) acolor(black%65) )     (scatter PD  D if Area==1,       mcolor(black)   msymbol(d))   (scatter PD  D if Area==2,   mcolor(black)    msymbol(S)) (scatter PD  D if Area==3,   mcolor(black)      msymbol(o)) (scatter PD  D if Area==4,   mcolor(black)    msymbol(T)) (scatter PD  D if Area==5,   mcolor(black)    msymbol(D)), name(a, replace)    legend(nobox position(12) rows(1)  order(3 "Anglo-Saxon" 4 "Continental" 5 "Eastern" 6 "Mediterranean" 7 "Nordic") span)   yscale(titlegap(*10)) ytitle("") xtitle("Discriminatory attitudes") note("{&rho} = `b' | {it:p-value} = `p' | {it:N} = `N6'.", size(vsmall)) title("{bf:Gender attitudes}")
  graph save "Graph/Figure1b", replace 
 
  
  * Combine the two graphs in a single pic
  
        		   grstyle init
	 grstyle set graphsize 13cm 24cm
 grc1leg  "Graph/Figure1a.gph" "Graph/Figure1b.gph",  cols(2) row(1) imargin(l=0 r=0 b=0) ring(1) pos(12) 
 graph export "Graph/Figure1.tif", replace
 
   *erase single graphs to save space
   
 erase "Graph/Figure1a.gph"
 erase "Graph/Figure1b.gph"
 
  